/*
 * filename：UserServiceImpl.java
 * copyright：Copyright by rocky
 * description: <br>
 */
package com.woolink.per.sys.user.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextImpl;

import com.woolink.per.common.Result;
import com.woolink.per.log.syslog.RunLog;
import com.woolink.per.sys.user.dao.UserDao;
import com.woolink.per.sys.user.service.UserService;

/**
 * 用户信息管理业务层
 * @author wuliquan
 * @version 2014-1-3
 * @see UserServiceImpl
 * @since
 */
public class UserServiceImpl implements UserService
{
    private RunLog runLog;
    
    private UserDao userDao;

    public Result queryMenus(HttpServletRequest request)
    {
        Result result = new Result();
        
        // 获取安全上下文
        SecurityContextImpl securityContextImpl = (SecurityContextImpl)request.getSession()
                .getAttribute("SPRING_SECURITY_CONTEXT");
        
        // 获取认证对象
        Authentication auth = securityContextImpl.getAuthentication();
        
        // 获取主体对象
        Object principal = auth.getPrincipal();
        
        //获得当前用户所拥有的权限
       List<GrantedAuthority> authorities = (List<GrantedAuthority>)securityContextImpl.getAuthentication().getAuthorities();
       
       // 确保菜单顺序展现，如果用HashSet则无法确保菜单展现是固定的
       Set menuSet = new LinkedHashSet();
       
       for (GrantedAuthority grantedAuthority : authorities)
       {
            System.out.println("UserInfoAction  170  ---------------Authority:"
                    + grantedAuthority.getAuthority());
            List<String> list =  userDao.loadUserResourcesByAuth(grantedAuthority.getAuthority());
            
            //合并所有的菜单List
            menuSet.addAll(list);
        }
       
       List<String> menus = new ArrayList<String>(menuSet);
       
       result.setResultList(menus);
        
       return result;
    }

    /**
     * @return Returns the runLog.
     */
    public RunLog getRunLog()
    {
        return runLog;
    }

    /**
     * @param runLog The runLog to set.
     */
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }

    /**
     * @return Returns the userDao.
     */
    public UserDao getUserDao()
    {
        return userDao;
    }

    /**
     * @param userDao The userDao to set.
     */
    public void setUserDao(UserDao userDao)
    {
        this.userDao = userDao;
    }
    
}
